using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MecRobGest.GUI
{
    public partial class GUIOF : Form
    {
        public GUIOF()
        {
            InitializeComponent();
        }

        private void GUIOF_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dBDataset.MaterialesOT' table. You can move, or remove it, as needed.
            this.materialesOTTableAdapter.Fill(this.dBDataset.MaterialesOT);
            // TODO: This line of code loads data into the 'dBDataset.Clients' table. You can move, or remove it, as needed.
            this.clientsTableAdapter.Fill(this.dBDataset.Clients);
            // TODO: This line of code loads data into the 'dBDataset.Materiales' table. You can move, or remove it, as needed.
            //this.materialesTableAdapter.Fill(this.dBDataset.Materiales);
            this.materialesTableAdapter.FillByOT(this.dBDataset.Materiales,0);
            // TODO: This line of code loads data into the 'dBDataset.OperarioOT' table. You can move, or remove it, as needed.
            this.operarioOTTableAdapter.Fill(this.dBDataset.OperarioOT);
            // TODO: This line of code loads data into the 'dBDataset.OrdenesTrabajo' table. You can move, or remove it, as needed.
            this.ordenesTrabajoTableAdapter.Fill(this.dBDataset.OrdenesTrabajo);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try{
                int iFila = dataGridView1.SelectedCells[0].RowIndex;
                seleccionaOrdre(dataGridView1.Rows[iFila].Cells[0].Value.ToString());
            }catch(Exception err){
                MessageBox.Show("No se ha podido seleccionar ninguna OF\r\n\r\nError:\r\n" + err.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void seleccionaOrdre(String sOrdre){
            int numOrdre = 0;
            this.tabControl1.SelectedTab = this.tabPage1;
            lblOrdre.Text = sOrdre;
            int.TryParse(sOrdre,out numOrdre);
            DataRow[] result = dBDataset.OrdenesTrabajo.Select("Id = "+ sOrdre + " ");
            if(result.Length == 1){
                lblCliente.Text = (String)result[0].ItemArray[1];
                lblIdentificacion.Text = (String)result[0].ItemArray[4];
                lblCantidad.Text = ((Double)result[0].ItemArray[6]).ToString();
                lblFechaRecepcion.Text = ((System.DateTime)result[0].ItemArray[2]).ToShortDateString();
                lblFechaEntrega.Text = ((System.DateTime)result[0].ItemArray[8]).ToShortDateString();
                txtDescripcionSel.Text = (String)result[0].ItemArray[5];
                txtNotasSel.Text = (String)result[0].ItemArray[9];
                txtCargoMaterial.Text = ((Double)result[0].ItemArray[10]).ToString();
                this.materialesTableAdapter.FillByOT(this.dBDataset.Materiales,numOrdre);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            int res = 0;
            int numero = 0;
            int identificacion = 0;
            int cantidad = 0;
            try{
                int.TryParse(txtNumero.Text,out numero);
                
                int.TryParse(txtCantidad.Text,out cantidad);
            }catch(Exception err){
                MessageBox.Show("Algun valor invalido!");
            }
            
            /*this.ordenesTrabajoTableAdapter.InsertQuery(((System.Data.DataRowView)cmbCliente.SelectedValue).Row.ItemArray[1].ToString(),
                                                        txtDataEntr.Value, 
                                                        numero,
                                                        txtIdentificacion.Text,
                                                        txtDescripcion.Text, 
                                                        cantidad,
                                                        0,
                                                        0,
                                                        0,
                                                        txtDataRec.Value, 
                                                        txtNotas.Text);*/
            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.ordenesTrabajoTableAdapter.Fill(this.dBDataset.OrdenesTrabajo);
            dataGridView1.Update();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            dataGridView1.BindingContext[this.dBDataset.OrdenesTrabajo].EndCurrentEdit();
            this.ordenesTrabajoTableAdapter.Update(this.dBDataset.OrdenesTrabajo);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            int iOt = 0;
            GUINewMateriales gMat = new GUINewMateriales();
            gMat.dView = dataGridView2;
            int.TryParse(lblOrdre.Text, out iOt);
            gMat.iOT = iOt;
            gMat.MdiParent = this.Parent.FindForm();
            
            gMat.Show();

        }

        private void button14_Click(object sender, EventArgs e)
        {
            int iOrdre = 0;
            int.TryParse(lblOrdre.Text,out iOrdre);
            this.materialesTableAdapter.FillByOT(this.dBDataset.Materiales,iOrdre);
            dataGridView1.Update();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            GUIOFReport gOFReport = new GUIOFReport();
            gOFReport.MdiParent = this.Parent.FindForm();
            gOFReport.Show();
        }
    }
}